package org.Tian;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUtil {
    //这里的static因为我们下面是静态代码块
    private static Properties properties=new Properties();
    //静态代码块
    static {
        //这里把InputStream写到括号里原因是jdk1.7后引入的语法，作用就是我们不需要自己去关闭资源
        try(InputStream inputStream = JdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties")){
            properties.load(inputStream);
            Class.forName(properties.getProperty("driver"));
            //这里是抛了两个异常
        }catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }
    }
    public static Connection getConnection()throws SQLException {
        return DriverManager.getConnection(properties.getProperty("url"),
                properties.getProperty("username"), properties.getProperty("password"));
    }
}
